package com.mall.malladminbackend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.malladminbackend.entity.Spu;
import com.mall.malladminbackend.entity.SpuImg;
import com.mall.malladminbackend.entity.SpuKey;
import com.mall.malladminbackend.mapper.SpuImgMapper;
import com.mall.malladminbackend.mapper.SpuKeyMapper;
import com.mall.malladminbackend.mapper.SpuMapper;
import com.mall.malladminbackend.model.PageBean;
import com.mall.malladminbackend.model.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 2022/8/15
 * 一杯茶，一根烟，一个bug解一天
 */
@Service
@AllArgsConstructor
@Transactional
public class SpuService {

     private SpuMapper spuMapper;
     private SpuImgMapper spuImgMapper;
     private SpuKeyMapper spuKeyMapper;


//   编辑

     public  R updateSpu(Spu spu){
//         int i = spuMapper.updateSpu(spu);
//         int i1 = spuImgMapper.deleteImg(spu.getId());
//         int i3 = spuKeyMapper.deleteSpukey(spu.getId());
//         int i4 = spuKeyMapper.addSpuKey(spu.getId(), spukey);
//         int i2 = spuImgMapper.allImgs(spu.getId(), spuImg);
//         return  (i1+i+i2+i3+i4)>0 ? R.buildSuccess(1,"success") : R.buildSuccess(100,"失败");
         // 修改spu
         int i = spuMapper.updateSpu(spu);
         //得到spu的id 直接删除所有轮播图在重新添加
         Long spuId = spu.getId();
         QueryWrapper<SpuImg> qw = new QueryWrapper<>();
         qw.eq("spu_id",spuId);
         spuImgMapper.delete(qw);
         //添加轮播图
         spu.getSpuImgs().forEach(img -> {
             img.setSpuId(spuId);
             spuImgMapper.insert(img);
         });
         //删除所有规格id
         QueryWrapper<SpuKey> qw1=new QueryWrapper();
         qw1.eq("spu_id",spuId);
         spuKeyMapper.delete(qw1);
         //添加规格
         spu.getSpecKeys().forEach(key -> {
             SpuKey spuKey = new SpuKey();
             spuKey.setSpuId(spuId);
             spuKey.setSpecKeyId(key.getId());
             spuKeyMapper.insert(spuKey);
         });
         return R.buildSuccess(1,"修改成功");
     }


//   spu查询
      public R getAllSpe(Integer page,Integer size){
          Page<Spu> startPage = PageHelper.startPage(page, size);
          spuMapper.getAllSpu();
          return R.buildSuccess(PageBean.buildPageBean(startPage));
      }
//   spu添加
      public R insertSpu(Spu spu,String[] spuImg,int spukey[]){
          int i = spuMapper.insertSpu(spu);
          int i1 = spuImgMapper.allImgs(spu.getId(), spuImg);
          int i2 = spuKeyMapper.addSpuKey(spu.getId(), spukey);
          return  (i1+i+i2)>0 ? R.buildSuccess(1,"success") : R.buildSuccess(100,"失败");
      }



  //  spu删除
     public  R delete(Integer id){
         int i = spuMapper.deleteSpu(id);
         int i1 = spuKeyMapper.deleteSpukey(id);
         int i2 = spuImgMapper.deleteImg(id);
         return  (i1+i+i2)>0 ? R.buildSuccess(1,"success") : R.buildSuccess(100,"失败");
     }
//    spu回显
     public  R UeditSpu(Integer id){
         Spu spu = spuMapper.selectallAndimgs(id);
         return R.buildSuccess(spu);
     }


}
